package com.yonyou.crm.bpub.biztype.appctrl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yonyou.crm.common.exception.CrmMessageException;
import com.yonyou.crm.common.rest.util.RequestUtil;
import com.yonyou.crm.common.rest.util.ResponseUtil;
import com.yonyou.crm.sys.modules.rmitf.IBiztypeRmService;

@RestController
@RequestMapping(value = "/{version}/mbpub/biztypes")
public class AppBiztypeCtrl {

	@Autowired 
	IBiztypeRmService service;
	
	/**
	 * 查询业务类型
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException 
	 */
	@RequestMapping(method = RequestMethod.GET)
	public Object list(@PathVariable("version") String version, HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {

		JSONObject paramObj = RequestUtil.getParamObject(request);
		
		String objName = paramObj.getString("type");
		//首字母大写
		objName = objName.substring(0, 1).toUpperCase() + objName.substring(1);

		Map<String, Object> biztype = service.getModuleBiztypesForCurrentRole(objName);
		
		List<Map<String, Object>> biztypeList = (List<Map<String, Object>>)biztype.get("biztypeList");
		
		for(Map<String, Object> biz : biztypeList) {
			biz.put("id", biz.get("key"));
			biz.put("type", biz.get("title"));
			biz.remove("key");
			biz.remove("title");
		}
		
		Map<String, Object> res = new HashMap<>();
		res.put("list", biztypeList);

		return ResponseUtil.getObject(res);
	}
	
}
